---@class GT.Inventory 物品栏
---@field ui table<number,GT.ui.control.item>

GT.Inventory = class()


function GT.Inventory:ctor()
    self.ui = {}
    self.itemTip = {}
    self.prefab = {}
    self.tickid = {}
    self.prefab["物品栏"] = GT.ui.prefab.obj({
        module = "ffe2c86b-3b1e-4a58-a0cc-6eb027613705",
        parent = "panel_2",
    })
    local p = self.prefab["物品栏"]
    self["self"] = p:child(GT.ui, "物品栏")
    self["self"].binding.x = 1300.0
    self["self"].binding.y = 125.0
    -- self["self"].binding.z_order = 99
    self.Inventorys = {
        { x = 49.0,  y = 198.0 },
        { x = 130.0, y = 198.0 },
        { x = 49.0,  y = 123 },
        { x = 130,   y = 123 },
        { x = 49.0,  y = 48 },
        { x = 130,   y = 48 },
    }
    for i, v in ipairs(self.Inventorys) do
        local index = i - 1
        self.ui[index] = GT.ui.control.item.obj({
            x = v.x,
            y = v.y,
            parent = self["self"].uid,
            absolute_scale = {
                x = 0.75,
                y = 0.75,
            },
            key = KeyboardKey[i],
            cast_key = KeyboardKey[i],
            item_enter_ck = function(data, p, ui)
                if ui.u[p.pid].slot_binding == nil then
                    return
                end
                local u = ui.u[p.pid].slot_binding.u
                if u ~= nil then
                    local item = u:api_get_item_by_slot(SlotType['BAR'], i)
                    if item ~= nil then
                        self:set_item_tip(p, item)
                    end
                end
            end,
            item_leave_ck = function(data, p, ui)
                self:hide(p)
            end
        })
    end

    ---物品提示UI
    self.itemTip["物品提示"] = p:child(GT.ui, "物品提示")
    self.itemTip["物品图标"] = p:child(GT.ui.control.image, "物品提示.物品图标.icon")
    self.itemTip["物品等级"] = p:child(GT.ui.control.label, "物品提示.lv")
    self.itemTip["物品名字"] = p:child(GT.ui.control.label, "物品提示.name")
    self.itemTip["物品描述"] = p:child(GT.ui.control.label, "物品提示.des.text")
end

---选择单位触发 绑定
---@param p GT.player
---@param u GT.unit
function GT.Inventory:select_unit(p, u)
    for k, v in pairs(self.ui) do
        v.u[p.pid].binding.slot_binding = {
            slot_index = k,
            slot_type = SlotType['BAR'],
            u = u
        }
    end
end

---设置物品提示
---@param p GT.player
---@param i GT.item
function GT.Inventory:set_item_tip(p, i)
    if i ~= nil then
        self.itemTip["物品图标"].u[p.pid].binding.img = i:get_icon_id_by_item_type()
        self.itemTip["物品等级"].u[p.pid].binding.text = 'lv.' .. i:api_get_level()
        self.itemTip["物品名字"].u[p.pid].binding.text = i:get_name()
        self.tickid[p.pid] = tick.add(function(uuid)
            self.itemTip["物品描述"].u[p.pid].binding.text = i:tip()
        end)
        self:show(p)
    end
end

---@param p GT.player
function GT.Inventory:show(p)
    self.itemTip["物品提示"].u[p.pid].binding.show = true
end

---@param p GT.player
function GT.Inventory:hide(p)
    self.itemTip["物品提示"].u[p.pid].binding.show = false
    if self.tickid[p.pid]~=nil then
        tick.del(self.tickid[p.pid])
    end
    
end
